package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.TreePath;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service("treepathParser")
public class TreePathParser extends AbstractComponentParser {
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode,Component component) {
		TreePath treePath=(TreePath)component;
	   super.setAttributes(parseContext, objectNode, component);
	   ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("onItemClickExpression");
			if(jsonNode!=null && !"null".equals(jsonNode.asText())&&!StringUtils.isEmpty(jsonNode.asText())) {
				String onItemClickExpression=jsonNode.asText();
				if(!StringUtils.isEmpty(onItemClickExpression)) {
					String expressionType=Expression.EXPRESSION_TYPE_JAVASCRIPT;
					if(onItemClickExpression.startsWith("@")) {
						expressionType=Expression.EXPRESSION_TYPE_SPRING;
					}
					Expression expression=ExpressionUtil.convertToExpression(onItemClickExpression,expressionType);
					treePath.setOnItemClickExpression(expression);
				}
			}
		}
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new TreePath();
	}

}
